package com.group.zsxm.web.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.config.ParentPackage;
import org.apache.struts2.config.Result;
import org.apache.struts2.config.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.group.core.Constants;
import com.group.core.common.Message;
import com.group.zsxm.entity.Xtuser;
import com.group.zsxm.service.DwsbService;
import com.group.zsxm.web.common.BaseAction;
@ParentPackage("appDefault")
@Scope("prototype")
@Controller
@Results( { 
			@Result(name = "input1", value = "/WEB-INF/jsp/dwsb/dwsb1.jsp"),
			@Result(name = "input2", value = "/WEB-INF/jsp/dwsb/dwsb2.jsp"),
			@Result(name = "input3", value = "/WEB-INF/jsp/dwsb/dwsb3.jsp"),
			@Result(name = "error", value = "/WEB-INF/jsp/dwsb/error.jsp"),
			@Result(name = "success", value = "/WEB-INF/jsp/frame/maingl.jsp"),
		})
public class DwsbAction extends BaseAction{
	
	@Autowired
	@Qualifier("dwsbService")
	private DwsbService dwsbService;
	
	private Xtuser xtuser ;
	
	private Map<String, String> dwsb;
	private Map<String, String> jbxx;
	private List<Map<String, String>> lxs;
	private String opttype;
	public String getOpttype() {
		return opttype;
	}

	public void setOpttype(String opttype) {
		this.opttype = opttype;
	}

	public Xtuser getXtuser() {
		return xtuser;
	}

	public void setXtuser(Xtuser xtuser) {
		this.xtuser = xtuser;
	}
	public void onPrepare() {
		xtuser = (Xtuser)getSession().getAttribute(Constants.USER_SESSION_KEY);
	} 
	
	public String execute(){
		jbxx = dwsbService.getDwsbJbxx(xtuser.getUserid());
		if(xtuser.getLoginbz()==1){
			lxs = dwsbService.getDwsblxList();
			dwsb = dwsbService.preDwsb1(xtuser.getUserid(), xtuser.getSbdm());
			return "input1";
		}else if(xtuser.getLoginbz()==2){
			dwsb = dwsbService.preDwsb2(xtuser.getUserid(), xtuser.getSbdm());
			return "input2";
		}else if(xtuser.getLoginbz()==3){
			dwsb = dwsbService.preDwsb3(xtuser.getUserid(), xtuser.getSbdm());
			return "input3";
		}
		return "error";
	}
	
	public void doSaveDwsb(){
		Map<String, Object> mapOut = new HashMap<String, Object>();
		try{
			if(xtuser.getLoginbz()==1){
				dwsbService.doSaveDwsb1(xtuser.getUserid(), xtuser.getSbdm(), dwsb);
			}else if(xtuser.getLoginbz()==2){
				dwsbService.doSaveDwsb2(xtuser.getUserid(), xtuser.getSbdm(), dwsb);
			}else if(xtuser.getLoginbz()==3){
				dwsbService.doSaveDwsb3(xtuser.getUserid(), xtuser.getSbdm(), dwsb);
			}
			if(opttype != null && opttype.equals("1")){
				//是保存上报\
				dwsbService.doDwsb(  xtuser.getUserid(),xtuser.getSbdm(),xtuser.getLoginbz());
			}
			message = new Message("1","保存成功");
		}catch(Exception e){
			message = new Message("-1",e.getMessage());
		}
		mapOut.put("message", message);
		this.renderJson(mapOut);
	}
	
	/**
	 * Mapping
	 * @return
	 */
	public Map<String, String> getDwsb() {
		return dwsb;
	}

	public void setDwsb(Map<String, String> dwsb) {
		this.dwsb = dwsb;
	}

	public List<Map<String, String>> getLxs() {
		return lxs;
	}

	public void setLxs(List<Map<String, String>> lxs) {
		this.lxs = lxs;
	}

	public Map<String, String> getJbxx() {
		return jbxx;
	}

	public void setJbxx(Map<String, String> jbxx) {
		this.jbxx = jbxx;
	}
	
}
